package com.kuaifu.autofare.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kuaifu.autofare.bean.model.ParkInfo;
import com.kuaifu.autofare.bean.model.ParkingEnterprise;
import com.kuaifu.autofare.bean.vo.resp.ParkTransDetailRespVo;
import com.kuaifu.autofare.bean.vo.resp.ParkTransTotalRespVo;
import com.kuaifu.autofare.common.utils.LogUtils;
import com.kuaifu.autofare.common.utils.StringUtils;
import com.kuaifu.autofare.dao.ParkingEnterpriseDao;
import com.kuaifu.autofare.dao.common.BaseDao;
import com.kuaifu.autofare.dao.common.FindRequestEntity;
import com.kuaifu.autofare.dao.common.WhereEnum;
import com.kuaifu.autofare.service.IParkInfoService;
import com.kuaifu.autofare.service.IParkingEnterpriseService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mocaijin on 21/12/2017.
 */
@Service
public class ParkingEnterpriseServiceImpl extends BaseServiceImpl<ParkingEnterprise> implements IParkingEnterpriseService {

    @Autowired
    private ParkingEnterpriseDao parkingEnterpriseDao;

    @Autowired
    private IParkInfoService parkInfoService;

    @Override
    public ParkingEnterprise get(ParkingEnterprise parkingEnterprise) throws Exception {

        FindRequestEntity findReq = new FindRequestEntity();
        if (!StringUtils.isBank(parkingEnterprise.getAppId())) {
            findReq.addWhere("app_id", WhereEnum.EQ, parkingEnterprise.getAppId());
        }
        if (!StringUtils.isBank(parkingEnterprise.getOpenId())) {
            findReq.addWhere("open_id", WhereEnum.EQ, parkingEnterprise.getOpenId());
        }
        if (!StringUtils.isBank(parkingEnterprise.getCustId())) {
            findReq.addWhere("cust_id", WhereEnum.EQ, parkingEnterprise.getCustId());
        }
        if (!StringUtils.isBank(parkingEnterprise.getEntNo())) {
            findReq.addWhere("ent_no", WhereEnum.EQ, parkingEnterprise.getEntNo());
        }
        if (!StringUtils.isBank(parkingEnterprise.getPassword())) {
            findReq.addWhere("password", WhereEnum.EQ, parkingEnterprise.getPassword());
        }

        List<ParkingEnterprise> parkingEnterpriseList = find(findReq);
        if (parkingEnterpriseList != null && parkingEnterpriseList.size() > 0) {
            return parkingEnterpriseList.get(0);
        }
        return null;
    }

    @Override
    public Map<String, String> bindWx(ParkingEnterprise parkingEnterprise) throws Exception {
        Map<String, String> resultMap = new HashMap<>();


        ParkingEnterprise result = new ParkingEnterprise();
        result.setEntNo(parkingEnterprise.getEntNo());
        result.setPassword(parkingEnterprise.getPassword());
        result = get(result);
        LogUtils.debug(JSONObject.toJSONString(result));
        if (result != null) {
            int i = parkingEnterpriseDao.update(parkingEnterprise);
        } else {
            resultMap.put("error", "账号或密码不正确，请重试");
        }


        return resultMap;
    }

    @Override
    public boolean unBindWx(ParkingEnterprise parkingEnterprise) {
        int i = parkingEnterpriseDao.unBindWx(parkingEnterprise);
        if (i == 1) {
            return true;
        }
        return false;
    }

    @Override
    public List<ParkingEnterprise> getParkingEnterprise(ParkingEnterprise parkingEnterprise) {
        return parkingEnterpriseDao.getParkingEnterprise(parkingEnterprise);
    }

    @Override
    public ParkingEnterprise getByCustId(int custId) {

        ParkingEnterprise parkingEnterprise = new ParkingEnterprise();
        parkingEnterprise.setCustId(custId + "");
        List<ParkingEnterprise> parkingEnterprises = parkingEnterpriseDao.getParkingEnterprise(parkingEnterprise);

        if (parkingEnterprises != null && parkingEnterprises.size() >= 1) {
            return parkingEnterprises.get(0);
        }

        return null;
    }

    @Override
    public ParkTransTotalRespVo getTransTotal(int custId, String date) throws Exception {
        ParkInfo parkInfo = new ParkInfo();
        String outTimeBegin = "";
        String outTimeEnd = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBank(date)) {

            outTimeBegin = simpleDateFormat.format(new Date());
            outTimeEnd = simpleDateFormat.format(DateUtils.addDays(new Date(), 1));
        } else {
            Date currentDate = simpleDateFormat.parse(date);
            outTimeBegin = date;
            outTimeEnd = simpleDateFormat.format(DateUtils.addDays(currentDate, 1));
        }

        parkInfo.setOutTimeDegin(outTimeBegin);
        parkInfo.setOutTimeEnd(outTimeEnd);
        parkInfo.setParkLotCustimerId(custId);

        List<Map<String, Integer>> mapList = parkInfoService.getDailyCharge(parkInfo);
        ParkTransTotalRespVo parkTransTotalRespVo = new ParkTransTotalRespVo();
        LogUtils.debug(JSONObject.toJSONString(mapList));
        //收费方式（0-未支付 1-现金 2-平台代扣）
        for (Map<String, Integer> map : mapList) {
            Integer chargeType = map.get("charge_type");
            Integer totalCnt = new Integer(String.valueOf(map.get("total_cnt")));
            Integer chargeAmount = new Integer(String.valueOf(map.get("charge_amount")));
            if (chargeType == 2) {
                parkTransTotalRespVo.setPlatformCount(totalCnt);
                parkTransTotalRespVo.setPlatformAmount(chargeAmount);
            } else if (chargeType == 1) {
                parkTransTotalRespVo.setCashCount(totalCnt);
                parkTransTotalRespVo.setCashAmount(chargeAmount);
            }
        }
        parkTransTotalRespVo.setCustId(custId);
        return parkTransTotalRespVo;
    }

    @Override
    public ParkTransDetailRespVo getTransDetail(int custId, String date) throws Exception {
        ParkTransDetailRespVo parkTransDetailRespVo = new ParkTransDetailRespVo();

        ParkInfo parkInfo = new ParkInfo();
        String outTimeBegin = "";
        String outTimeEnd = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBank(date)) {

            outTimeBegin = simpleDateFormat.format(new Date());
            outTimeEnd = simpleDateFormat.format(DateUtils.addDays(new Date(), 1));
        } else {
            Date currentDate = simpleDateFormat.parse(date);
            outTimeBegin = date;
            outTimeEnd = simpleDateFormat.format(DateUtils.addDays(currentDate, 1));
        }

        parkInfo.setOutTimeDegin(outTimeBegin);
        parkInfo.setOutTimeEnd(outTimeEnd);
        parkInfo.setParkLotCustimerId(custId);
        //收费方式（0-未支付 1-现金 2-平台代扣）
        parkInfo.setChargeType(1);
        List<ParkInfo> cashList = parkInfoService.getParkInfo(parkInfo);
        parkTransDetailRespVo.setCashList(cashList);

        parkInfo.setChargeType(2);
        List<ParkInfo> platformList = parkInfoService.getParkInfo(parkInfo);
        parkTransDetailRespVo.setPlatformList(platformList);

        return parkTransDetailRespVo;
    }

    @Override
    public BaseDao<ParkingEnterprise> getBaseDao() {
        return parkingEnterpriseDao;
    }
}
